package features.advance.jdk.stream;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author LIN
 * @date 2021-07-16
 */
public class StreamDemo {

    public static void main(String[] args) throws Exception {
        int[] arr = {1,2,3};
        String a = "a";
        String b = "b";
        String c = "c";

        List<Object> ints = Arrays.asList(2,3,4);

        System.out.println(ints);

        List<String> strings = Arrays.asList(a, b, c);
        System.out.println(strings);

        b = "test";
        // 获取String类的char数组value
        Field value = b.getClass().getDeclaredField("value");
        // 设置指定字段可以访问
        value.setAccessible(true);
        char[] o = (char[])value.get(b);
        System.out.println(Arrays.toString(o));

        char[] chars = b.toCharArray();

        System.out.println(Arrays.toString(chars));

        chars[0] = 'T';
        System.out.println(b);
        // System.arraycopy当元素是基本类型或者String类型的时候，是深拷贝，
        // 当元素是引用类型的时候，就是浅拷贝
        System.out.println(strings);

        A a1 = new A(1);
        A a2 = new A(2);
        A a3 = new A(3);

        List<A> as = Arrays.asList(a1, a2, a3);

        System.out.println(as);

        a1.a = 100;

        System.out.println(as);

        List<Integer> list = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9).collect(Collectors.toList());
        System.out.println(list);
    }

    @SafeVarargs
    public static <T> List<T> asList(T... a) {
        return null;
    }

}

class A{
    protected int a;

    public A(int a){
        this.a = a;
    }

    @Override
    public String toString() {
        return "A{" +
                "a=" + a +
                '}';
    }
}
